package com.authority.test.dao;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


/*import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.AccessFlag;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo;*/

public class GenerateDaoImpl {

	private Class[] cs = new Class[] { this.getClass() };
	private String[] prefixStrings = new String[] { "user_manager" };

	public static void main(String[] args) {
		try {
			GenerateDaoImpl generate = new GenerateDaoImpl();
/*			List<Bean> aa = generate.test();
			for (Bean a : aa) {
				//generate.generateMehodContext(a);
				System.out.println(generate.generateMehodContext(a));
				System.out.println();
				System.out.println();
			}*/
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public StringBuilder generateMehodContext(Bean a) {
		StringBuilder sb = new StringBuilder();
		System.out.println(a.MethodName);
		if (a.MethodName.toUpperCase().contains("INSERT")) {
			sb.append("try{ return (").append(a.MethodReturnType);
			sb.append(")sqlMapClient.insert(\"").append(a.MethodName).append("\" ");
			for (String s : a.MethodInputName) {
				sb.append(",").append(s);
			}
			sb.append(");");
			sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
					.append(" error\",e); } return null;");
		} else if (a.MethodName.toUpperCase().contains("UPDATE")) {
			if(a.MethodInputName.length>1){
				sb.append("try{ ");
				sb.append("Map<String, Object> obj = new HashMap<String, Object>();");
				for (String s : a.MethodInputName) {
					sb.append("obj.put(\"").append(s).append("\" , ").append(s).append(");");
				}
				sb.append("sqlMapClient.update(\"").append(a.MethodName).append("\" ,obj");
				sb.append(");");
				sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
						.append(" error\",e); } return 0;");
			}else{
				sb.append("try{ return ").append("sqlMapClient.update(\"").append(a.MethodName).append("\" ");
				for (String s : a.MethodInputName) {
					sb.append(",").append(s);
				}
				sb.append(");");
				sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
						.append(" error\",e); } return 0;");
			}
			
		} else if (a.MethodName.toUpperCase().contains("DELETE")) {
			if(a.MethodInputName.length>1){
				sb.append("try{ ");
				sb.append("Map<String, Object> obj = new HashMap<String, Object>();");
				for (String s : a.MethodInputName) {
					sb.append("obj.put(\"").append(s).append("\" , ").append(s).append(");");
				}
				sb.append("return sqlMapClient.delete(\"").append(a.MethodName).append("\" ,obj");
				sb.append(");");
				sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
						.append(" error\",e); } return 0;");
			}else{
				sb.append("try{ return ").append("sqlMapClient.delete(\"").append(a.MethodName).append("\" ");
				for (String s : a.MethodInputName) {
					sb.append(",").append(s);
				}
				sb.append(");");
				sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
						.append(" error\",e); } return 0;");
			}
			
		}else if (a.MethodName.toUpperCase().contains("QUERY")) {
			if(a.MethodInputName.length>1){
				sb.append("try{ ");
				sb.append("Map<String, Object> obj = new HashMap<String, Object>();");
				for (String s : a.MethodInputName) {
					sb.append("obj.put(\"").append(s).append("\" , ").append(s).append(");");
				}
				sb.append("sqlMapClient.queryForList(\"").append(a.MethodName).append("\" ,obj");
				sb.append(");");
				sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
						.append(" error\",e); } return null;");
			}else if(a.MethodReturnType.toUpperCase().equals("LIST")){
				sb.append("try{ return ").append("sqlMapClient.queryForList(\"").append(a.MethodName).append("\" ");
				for (String s : a.MethodInputName) {
					sb.append(",").append(s);
				}
				sb.append(");");
				sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
						.append(" error\",e); } return null;");
			}else{
				sb.append("try{ return (").append(a.MethodReturnType).append(")sqlMapClient.queryForObject(\"").append(a.MethodName).append("\" ");
				for (String s : a.MethodInputName) {
					sb.append(",").append(s);
				}
				sb.append(");");
				sb.append("} catch (SQLException e) { e.printStackTrace(); logger.error(\"").append(a.MethodName)
						.append(" error\",e); } return null;");
			}
			
		}
		return sb;
	}
/*
	public List<Bean> test() throws NotFoundException {
		List<Bean> as = new ArrayList<Bean>();
		ClassPool pool = ClassPool.getDefault();

		for (int i = 0; i < cs.length; i++) {
			CtClass ctClass;
			Class clz = cs[i];
			ctClass = pool.get(clz.getName());
			Method[] methods = clz.getDeclaredMethods();
			for (Method m : methods) {
				String name = m.getName();
				CtMethod cm = ctClass.getDeclaredMethod(name);
				MethodInfo methodInfo = cm.getMethodInfo();
				Class[] parameterTypes = m.getParameterTypes();
				CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
				LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
						.getAttribute(LocalVariableAttribute.tag);
				if (attr == null) {
				}
				if (cm.getMethodInfo().getAccessFlags() != AccessFlag.PUBLIC)
					continue;
				String[] paramNames = new String[cm.getParameterTypes().length];
				int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
				for (int j = 0; j < paramNames.length; j++)
					paramNames[j] = attr.variableName(j + pos);

				Bean aaa = new Bean();
				aaa.MethodName = prefixStrings[i] + "." + name;
				aaa.MethodReturnType = m.getReturnType().getSimpleName();
				String[] methodInputName = new String[paramNames.length];
				String[] methodInputType = new String[paramNames.length];
				for (int i1 = 0; i1 < paramNames.length; i1++) {
					//System.out.println(parameterTypes[i1].getSimpleName()+"&&&&&&&&&&&&&&&&"+paramNames[i1]);
					methodInputType[i1] = parameterTypes[i1].getSimpleName();
					methodInputName[i1] = paramNames[i1];
					as.add(aaa);
				}
				aaa.MethodInputType = methodInputType;
				aaa.MethodInputName = methodInputName;
			}
		}
		return as;
	}
*/
	public class Bean {

		String MethodReturnType;

		String MethodName;

		String[] MethodInputType;

		String[] MethodInputName;
	}
}
